<?php
if(!current_user_can("manage_options")){
                   show_admin_bar(false);
              }
add_action( 'after_setup_theme', 'et_setup_theme' );
if ( ! function_exists( 'et_setup_theme' ) ){
	function et_setup_theme(){
		global $themename, $shortname;
		$themename = "Chameleon";
		$shortname = "chameleon";

		require_once(TEMPLATEPATH . '/epanel/custom_functions.php');

		require_once(TEMPLATEPATH . '/includes/functions/comments.php');

		require_once(TEMPLATEPATH . '/includes/functions/sidebars.php');

		load_theme_textdomain('Chameleon',get_template_directory().'/lang');

		require_once(TEMPLATEPATH . '/epanel/options_chameleon.php');

		require_once(TEMPLATEPATH . '/epanel/core_functions.php');

		require_once(TEMPLATEPATH . '/epanel/post_thumbnails_chameleon.php');

		include(TEMPLATEPATH . '/includes/widgets.php');
	}
}

add_action('wp_head','et_portfoliopt_additional_styles',100);
function et_portfoliopt_additional_styles(){ ?>
	<style type="text/css">
		#et_pt_portfolio_gallery { margin-left: -41px; margin-right: -51px; }
		.et_pt_portfolio_item { margin-left: 35px; }
		.et_portfolio_small { margin-left: -40px !important; }
		.et_portfolio_small .et_pt_portfolio_item { margin-left: 32px !important; }
		.et_portfolio_large { margin-left: -26px !important; }
		.et_portfolio_large .et_pt_portfolio_item { margin-left: 11px !important; }
	</style>
<?php }

function register_main_menus() {
	register_nav_menus(
		array(
			'primary-menu' => __( 'Primary Menu' )
		)
	);
}
if (function_exists('register_nav_menus')) add_action( 'init', 'register_main_menus' );

if ( ! function_exists( 'et_list_pings' ) ){
	function et_list_pings($comment, $args, $depth) {
		$GLOBALS['comment'] = $comment; ?>
		<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?> - <?php comment_excerpt(); ?>
	<?php }
}

// add Home link to the custom menu WP-Admin page
function et_add_home_link( $args ) {
	$args['show_home'] = true;
	return $args;
}
add_filter( 'wp_page_menu_args', 'et_add_home_link' );

add_action('wp_head','et_add_meta_javascript');
function et_add_meta_javascript(){
	global $shortname;
	echo '<!-- used in scripts -->';
	echo '<meta name="et_featured_auto_speed" content="'.get_option($shortname.'_slider_autospeed').'" />';

	$disable_toptier = get_option($shortname.'_disable_toptier') == 'on' ? 1 : 0;
	echo '<meta name="et_disable_toptier" content="'.$disable_toptier.'" />';

	$featured_slider_auto = get_option($shortname.'_slider_auto') == 'on' ? 1 : 0;
	echo '<meta name="et_featured_slider_auto" content="'.$featured_slider_auto.'" />';

	echo '<meta name="et_theme_folder" content="'.get_bloginfo('template_directory').'" />';
}


add_action('et_header_top','et_chameleon_control_panel');
function et_chameleon_control_panel(){
	$admin_access = apply_filters( 'et_showcontrol_panel', current_user_can('switch_themes') );
	if ( !$admin_access ) return;
	if ( get_option('chameleon_show_control_panel') <> 'on' ) return;
	global $et_bg_texture_urls, $et_google_fonts; ?>
	<div id="et-control-panel">
		<div id="control-panel-main">
			<a id="et-control-close" href="#"></a>
			<div id="et-control-inner">
				<h3 class="control_title">Example Colors</h3>
				<a href="#" class="et-control-colorpicker" id="et-control-background"></a>

				<div class="clear"></div>

				<?php
					$sample_colors = array( '6a8e94', '8da49c', 'b0b083', '859a7c', 'c6bea6', 'b08383', 'a4869d', 'f5f5f5', '4e4e4e', '556f6a', '6f5555', '6f6755' );
					for ( $i=1; $i<=12; $i++ ) { ?>
						<a class="et-sample-setting" id="et-sample-color<?php echo $i; ?>" href="#" rel="<?php echo $sample_colors[$i-1]; ?>" title="#<?php echo esc_attr($sample_colors[$i-1]); ?>"><span class="et-sample-overlay"></span></a>
				<?php } ?>
				<p>or define your own in ePanel</p>

				<h3 class="control_title">Texture Overlays</h3>
				<div class="clear"></div>

				<?php
					$sample_textures = $et_bg_texture_urls;
					for ( $i=1; $i<=count($et_bg_texture_urls); $i++ ) { ?>
						<a title="<?php echo esc_attr($sample_textures[$i-1]); ?>" class="et-sample-setting et-texture" id="et-sample-texture<?php echo $i; ?>" href="#" rel="bg<?php echo $i+1; ?>"><span class="et-sample-overlay"></span></a>
				<?php } ?>

				<p>or define your own in ePanel</p>

				<?php
					$google_fonts = $et_google_fonts;
					$font_setting = 'Kreon';
					$body_font_setting = 'Droid+Serif';
					if ( isset( $_COOKIE['et_chameleon_header_font'] ) ) $font_setting = $_COOKIE['et_chameleon_header_font'];
					if ( isset( $_COOKIE['et_chameleon_body_font'] ) ) $body_font_setting = $_COOKIE['et_chameleon_body_font'];
				?>

				<h3 class="control_title">Fonts</h3>
				<div class="clear"></div>

				<label for="et_control_header_font">Header
					<select name="et_control_header_font" id="et_control_header_font">
						<?php foreach( $google_fonts as $google_font ) { ?>
							<?php $encoded_value = urlencode($google_font); ?>
							<option value="<?php echo esc_attr($encoded_value); ?>" <?php selected( $font_setting, $encoded_value ); ?>><?php echo esc_html($google_font); ?></option>
						<?php } ?>
					</select>
				</label>
				<a href="#" class="et-control-colorpicker et-font-control" id="et-control-headerfont_bg"></a>
				<div class="clear"></div>

				<label for="et_control_body_font">Body
					<select name="et_control_body_font" id="et_control_body_font">
						<?php foreach( $google_fonts as $google_font ) { ?>
							<?php $encoded_value = urlencode($google_font); ?>
							<option value="<?php echo esc_attr($encoded_value); ?>" <?php selected( $body_font_setting, $encoded_value ); ?>><?php echo esc_html($google_font); ?></option>
						<?php } ?>
					</select>
				</label>
				<a href="#" class="et-control-colorpicker et-font-control" id="et-control-bodyfont_bg"></a>
				<div class="clear"></div>

			</div> <!-- end #et-control-inner -->
		</div> <!-- end #control-panel-main -->
	</div> <!-- end #et-control-panel -->
<?php
}

add_action( 'template_redirect', 'et_load_chameleon_scripts' );
function et_load_chameleon_scripts(){
	$et_slider_type = apply_filters( 'et_slider_type', get_option('chameleon_slider_type') );
	$template_dir = get_bloginfo('template_directory');

	wp_enqueue_script('jquery_cycle', $template_dir . '/js/jquery.cycle.all.min.js', array('jquery'), '1.0', false);

	if ( $et_slider_type == 'nivo' )
		wp_enqueue_script('jquery_nivo', $template_dir . '/js/jquery.nivo.slider.pack.js', array('jquery'), '1.0', true);

	wp_enqueue_script($et_slider_type . '_script', $template_dir . '/js/et_'.$et_slider_type.'.js', array('jquery'), '1.0', true);

	$admin_access = apply_filters( 'et_showcontrol_panel', current_user_can('switch_themes') );
	if ( $admin_access && get_option('chameleon_show_control_panel') == 'on' ) {
		wp_enqueue_script('et_colorpicker', $template_dir . '/epanel/js/colorpicker.js', array('jquery'), '1.0', true);
		wp_enqueue_script('et_eye', $template_dir . '/epanel/js/eye.js', array('jquery'), '1.0', true);
		wp_enqueue_script('et_cookie', $template_dir . '/js/jquery.cookie.js', array('jquery'), '1.0', true);
		wp_enqueue_script('et_control_panel', get_bloginfo('template_directory') . '/js/et_control_panel.js', array('jquery'), '1.0', true);
	}
}

add_action( 'wp_head', 'et_set_bg_properties' );
function et_set_bg_properties(){
	global $et_bg_texture_urls;

	$bgcolor = '';
	$bgcolor = ( isset( $_COOKIE['et_chameleon_bgcolor'] ) && get_option('chameleon_show_control_panel') == 'on' ) ? $_COOKIE['et_chameleon_bgcolor'] : get_option('chameleon_bgcolor');

	$bgtexture_url = '';
	$bgimage_url = '';
	if ( get_option('chameleon_bgimage') == '' ) {
		if ( isset( $_COOKIE['et_chameleon_texture_url'] ) && get_option('chameleon_show_control_panel') == 'on' ) $bgtexture_url =  $_COOKIE['et_chameleon_texture_url'];
		else {
			$bgtexture_url = get_option('chameleon_bgtexture_url');
			if ( $bgtexture_url == 'Default' ) $bgtexture_url = '';
			else $bgtexture_url = get_bloginfo('template_directory') . '/images/body-bg' . ( array_search( $bgtexture_url, $et_bg_texture_urls )+2 ) . '.png';
		}
	} else {
		$bgimage_url = get_option('chameleon_bgimage');
	}

	$style = '';
	$style .= '<style type="text/css">';
	if ( $bgcolor <> '' ) $style .= 'body { background-color: #' . esc_attr($bgcolor) . '; }';
	if ( $bgtexture_url <> '' ) $style .= 'body { background-image: url(' . esc_attr($bgtexture_url) . '); }';
	if ( $bgimage_url <> '' ) $style .= 'body { background-image: url(' . esc_attr($bgimage_url) . '); background-position: top center; background-repeat: no-repeat; }';
	$style .= '</style>';

	if ( $bgcolor <> '' || $bgtexture_url <> '' || $bgimage_url <> '' ) echo $style;
}

add_action( 'wp_head', 'et_set_font_properties' );
function et_set_font_properties(){
	$font_style = '';
	$font_color = '';
	$font_family = '';
	$font_color_string = '';

	if ( isset( $_COOKIE['et_chameleon_header_font'] ) && get_option('chameleon_show_control_panel') == 'on' ) $et_header_font =  $_COOKIE['et_chameleon_header_font'];
	else {
		$et_header_font = get_option('chameleon_header_font');
		if ( $et_header_font == 'Droid+Serif' ) $et_header_font = '';
	}

	if ( isset( $_COOKIE['et_chameleon_header_font_color'] ) && get_option('chameleon_show_control_panel') == 'on' )
		$et_header_font_color =  $_COOKIE['et_chameleon_header_font_color'];
	else
		$et_header_font_color = get_option('chameleon_header_font_color');

	if ( $et_header_font <> '' || $et_header_font_color <> '' ) {
		$et_header_font_id = strtolower( str_replace( '+', '_', $et_header_font ) );
		$et_header_font_id = str_replace( ' ', '_', $et_header_font_id );

		if ( $et_header_font <> '' ) {
			$font_style .= "<link id='" . esc_attr($et_header_font_id) . "' href='http://fonts.googleapis.com/css?family=" . $et_header_font . "' rel='stylesheet' type='text/css' />";
			$font_family = "font-family: 'Droid Serif',Arial,Verdana,sans-serif;";
		}

		if ( $et_header_font_color <> '' ) {
			$font_color_string = "color: #" . esc_attr($et_header_font_color) . " !important; ";
		}

		$font_style .= "<style type='text/css'>h1,h2,h3,h4,h5,h6,ul#top-menu a { ". $font_family .  " }</style>";
		$font_style .= "<style type='text/css'>h1,h2,h3,h4,h5,h6,ul#top-menu > li.current_page_item > a, ul#top-menu a:hover, ul#top-menu > li.sfHover > a, h2.title a { ". esc_attr($font_color_string) .  " }
		#featured h2 a, #featured-modest h2 a, #featured h2, #featured-modest h2 { color: #fff !important; }
		</style>";

		echo $font_style;
	}

	$font_style = '';
	$font_color = '';
	$font_family = '';
	$font_color_string = '';

	if ( isset( $_COOKIE['et_chameleon_body_font'] ) && get_option('chameleon_show_control_panel') == 'on' ) $et_body_font =  $_COOKIE['et_chameleon_body_font'];
	else {
		$et_body_font = get_option('chameleon_body_font');
		if ( $et_body_font == 'Droid+Serif' ) $et_body_font = '';
	}

	if ( isset( $_COOKIE['et_chameleon_body_font_color'] ) && get_option('chameleon_show_control_panel') == 'on' )
		$et_body_font_color =  $_COOKIE['et_chameleon_body_font_color'];
	else
		$et_body_font_color = get_option('chameleon_body_font_color');

	if ( $et_body_font <> '' || $et_body_font_color <> '' ) {
		$et_body_font_id = strtolower( str_replace( '+', '_', $et_body_font ) );
		$et_body_font_id = str_replace( ' ', '_', $et_body_font_id );

		if ( $et_body_font <> '' ) {
			$font_style .= "<link id='" . esc_attr($et_body_font_id) . "' href='http://fonts.googleapis.com/css?family=" . $et_body_font . "' rel='stylesheet' type='text/css' />";
			$font_family = "font-family: 'Droid Serif',Arial,Verdana,sans-serif;";
		}

		if ( $et_body_font_color <> '' ) {
			$font_color_string = "color: #" . esc_attr($et_body_font_color) . " !important; ";
		}

		$font_style .= "<style type='text/css'>body { ". $font_family .  " }</style>";
		$font_style .= "<style type='text/css'>body { ". esc_attr($font_color_string) .  " }</style>";

		echo $font_style;
	}
}

add_filter( 'et_get_additional_color_scheme', 'et_remove_additional_stylesheet' );
function et_remove_additional_stylesheet( $stylesheet ){
	global $default_colorscheme;
	return $default_colorscheme;
} 

function ses_init() {
if (!session_id())
  session_start();
}
add_action('init','ses_init');


// Thay doi logo admin wordpress
//<?php bloginfo('template_directory'); 
function login_css() {
wp_enqueue_style( 'login_css',get_template_directory_uri().'css/login.css'); // duong dan den file css moi
}
add_action('login_head', 'login_css');



// No catalogue
// Refresh rules on activation/deactivation/category changes
register_activation_hook(__FILE__, 'no_category_base_refresh_rules');
add_action('created_category', 'no_category_base_refresh_rules');
add_action('edited_category', 'no_category_base_refresh_rules');
add_action('delete_category', 'no_category_base_refresh_rules');
function no_category_base_refresh_rules() {
	global $wp_rewrite;
	$wp_rewrite -> flush_rules();
}

register_deactivation_hook(__FILE__, 'no_category_base_deactivate');
function no_category_base_deactivate() {
	remove_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
	// We don't want to insert our custom rules again
	no_category_base_refresh_rules();
}

// Remove category base
add_action('init', 'no_category_base_permastruct');
function no_category_base_permastruct() {
	global $wp_rewrite, $wp_version;
	if (version_compare($wp_version, '3.4', '<')) {
		// For pre-3.4 support
		$wp_rewrite -> extra_permastructs['category'][0] = '%category%';
	} else {
		$wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
	}
}

// Add our custom category rewrite rules
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
	//var_dump($category_rewrite); // For Debugging

	$category_rewrite = array();
	$categories = get_categories(array('hide_empty' => false));
	foreach ($categories as $category) {
		$category_nicename = $category -> slug;
		if ($category -> parent == $category -> cat_ID)// recursive recursion
			$category -> parent = 0;
		elseif ($category -> parent != 0)
			$category_nicename = get_category_parents($category -> parent, false, '/', true) . $category_nicename;
		$category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
		$category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
		$category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
	}
	// Redirect support from Old Category Base
	global $wp_rewrite;
	$old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
	$old_category_base = trim($old_category_base, '/');
	$category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?category_redirect=$matches[1]';

	//var_dump($category_rewrite); // For Debugging
	return $category_rewrite;
}

// For Debugging
//add_filter('rewrite_rules_array', 'no_category_base_rewrite_rules_array');
//function no_category_base_rewrite_rules_array($category_rewrite) {
//	var_dump($category_rewrite); // For Debugging
//}

// Add 'category_redirect' query variable
add_filter('query_vars', 'no_category_base_query_vars');
function no_category_base_query_vars($public_query_vars) {
	$public_query_vars[] = 'category_redirect';
	return $public_query_vars;
}

// Redirect if 'category_redirect' is set
add_filter('request', 'no_category_base_request');
function no_category_base_request($query_vars) {
	//print_r($query_vars); // For Debugging
	if (isset($query_vars['category_redirect'])) {
		$catlink = trailingslashit(get_option('home')) . user_trailingslashit($query_vars['category_redirect'], 'category');
		status_header(301);
		header("Location: $catlink");
		exit();
	}
	return $query_vars;
}

// end // No catalogue



// check function user
// call : user_regvis_message();
function user_regvis_message() 
{
	/*
get_currentuserinfo() ;
global $user_level;
if ($user_level > 0) {
 echo "Wellcome visiter"
} else {
 echo "Wellcome Member"
}


     global $userdata;
    get_currentuserinfo();
    if ($userdata->user_level > 0) 
    {
    	echo "Wellcome visiter"
      if(function_exists('get_cimyFieldValue')) {
        $value = get_cimyFieldValue($userdata->ID, 'FIELDNAME');
        if ($value == 'YES') { $ads = 'off'; }
      }
    } else {
       echo "Wellcome Member"
       }
*/
}


// tao option menu

add_action('admin_menu', 'nano_menu_page');

function nano_menu_page() {
    add_theme_page('Nano Theme Option', 'Option Category Home View ', 'manage_options', 'nano-theme-option', 'nano_setting_page');
}
add_action('admin_init','nano_register_setting');

function nano_register_setting() {
    register_setting('nano-group','nano-num-post');
    register_setting('nano-group','nano-cat-1');
    register_setting('nano-group','nano-cat-2');
    register_setting('nano-group','nano-cat-3');
    register_setting('nano-group','silde-home-cata');
    register_setting('nano-group','nano-facebook');
}

function nano_setting_page() { ?>
    <div class="wrap">
        <?php screen_icon(); ?>
        <h2>Nano setting view category home view  </h2>
        <form id="nano_setting" method="post" action="options.php">
        <?php settings_fields('nano-group'); ?>
            <table class="nano_page">
                <tr valign="top">
                    <th scope="row">Limit Number Post .  Exp view 3 post/page home : 3 </th>
                    <td><input width="200" type="text" value="<?php echo get_option('nano-num-post'); ?>" name="nano-num-post" /></td>
                </tr>
                
                <tr valign="top">
                    <th scope="row">Homepage name categories . Exp  category 1 : tin tức khoa học </th>
                    <td><input style='width: 200px;' type="text" value="<?php echo get_option('nano-cat-1'); ?>" name="nano-cat-1" /></td>
                </tr>
                 <tr valign="top">
                    <th scope="row">                         . Exp  category 2:  trang thiết bị .  </th>
                    <td><input style='width: 200px;' type="text" value="<?php echo get_option('nano-cat-2'); ?>" name="nano-cat-2" /></td>
                </tr>
                 <tr valign="top">
                    <th scope="row">                         . Exp  category 3: thư viện ảnh.  </th>
                    <td><input style='width: 200px;' type="text" value="<?php echo get_option('nano-cat-3'); ?>" name="nano-cat-3" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row"> Silde home category .  </th>
                    <td><input style='width: 200px;' type="text" value="<?php echo get_option('silde-home-cata'); ?>" name="silde-home-cata" /></td>
                </tr>
                <!--
                <tr valign="top">
                    <th scope="row">Facebook Plugin</th>
                    <td><textarea name="nano-facebook" cols="60" rows="8"><?php echo get_option('nano-facebook'); ?></textarea></td>
                </tr>
                -->
            </table>
            <?php submit_button('save home category view'); ?>
        </form>
    </div>
<?php }
?>